package com.xiaodou.mjf.application.core.service.impl;

import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import javax.transaction.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;

import com.xiaodou.mjf.application.core.repository.FinancingRepository;
import com.xiaodou.mjf.application.core.service.FinancingService;
import com.xiaodou.mjf.entity.core.Financing;
import com.xiaodou.mjf.infrastructure.common.app.JpaSimpleService;

/***
 *
 * Financing业务访问实现
 * 
 * @author XiaoDou.com Code Generator
 * @version 1.0 2014-10-29 17:37:52
 *
 */
@Service
@Transactional
public class FinancingServiceImpl extends JpaSimpleService<Financing, String>
		implements FinancingService {

	Logger logger = LoggerFactory.getLogger(FinancingServiceImpl.class);

	@Resource
	FinancingRepository financingRepository;

	@Override
	protected JpaRepository<Financing, String> getRepository() {
		return financingRepository;
	}

	@Override
	public Page<Financing> pageAll(Financing entity) {
		return financingRepository.findAll(
				(root, query, cb) -> {

					Predicate p1 = cb.equal(root.get("status")
							.as(Integer.class), entity.getStatus());
					Predicate p2 = cb.like(root.get("title").as(String.class),
							"%" + entity.getTitle() + "%");
					Predicate p3 = cb.equal(root.get("type").as(Integer.class),
							entity.getType());

					if (entity.getStatus().equals(-1)) {
						return query.where(p2, p3).getRestriction();
					}
					/*
					 * if(entity.getLoan() != null && entity.getLoan().getTerm()
					 * != null && entity.getLoan().getTerm() != -1){
					 * Join<Financing,Loan> loanJoin =
					 * financeRoot.join(financeRoot
					 * .getModel().getSingularAttribute
					 * ("loan",Loan.class),JoinType.INNER); Predicate p3 =
					 * criteriaBuilder
					 * .equal(loanJoin.get("term").as(Integer.class),
					 * entity.getLoan().getTerm()); p1 =
					 * criteriaQuery.where(p1,p3).getRestriction(); }
					 */

					return query.where(p1, p2, p3).getRestriction();
				},
				preparePage(entity, new Sort(new Sort.Order(Sort.Direction.ASC,
						"top"),
						new Sort.Order(Sort.Direction.DESC, "startTime"))));
	}

	@Override
	public Page<Financing> pageFullScale(Financing entity) {
		return financingRepository.findAll(
				(root, query, cb) -> {
					Predicate p1 = cb.equal(
							root.get("investStatus").as(Integer.class),
							entity.getInvestStatus());
					Predicate p2 = cb.like(
							root.get("title").as(String.class),
							"%"+entity.getTitle()+"%");
					Predicate p3 = cb.equal(
							root.get("type").as(Integer.class),
							entity.getType());
					Predicate p4 = cb.equal(
							root.get("status").as(Integer.class),
							entity.getStatus());
					return query.where(p1,p2,p3,p4).getRestriction();
				},
				preparePage(entity, new Sort(new Sort.Order(Sort.Direction.ASC,
						"top"),
						new Sort.Order(Sort.Direction.DESC, "startTime"))));
	}

	@Override
	public Double getTotalAmountByTerm(int term) {

		return financingRepository.getTotalAmountByTerm(term,
				Financing.TYPE_XIAODOU);

	}

	@Override
	public Page<Financing> page(Financing entity) {
		return financingRepository.findAll(
				(root, query, cb) -> {
					Predicate p1 = cb.equal(root.get("type").as(Integer.class),
							entity.getType());
					Predicate p2 = cb.equal(root.get("status")
							.as(Integer.class), entity.getStatus());
					return query.where(p1, p2).getRestriction();
				},
				preparePage(entity, new Sort(new Sort.Order(Sort.Direction.ASC,
						"createTime"))));
	}

	@Override
	public Financing getFinancingByTermAndInvsetStatus(int term,
			int invsetStatus, Integer type) {

		return financingRepository.getFinancingByTermAndInvsetStatus(term,
				invsetStatus, type);

	}

	@Override
	public Object[][] productTotal() {
		return financingRepository.productTotal();
	}

	@Override
	public List<Financing> getFinancingListByStatus(Integer status) {
		return financingRepository.getFinancingListByStatus(status);
	}

    @Override
    public List<Financing> getFinancingListByInvestStatus(Integer investStatus) {
        return financingRepository.getFinancingListByInvestStatus(investStatus);
    }

	@Override
	public Financing getFinancingByTermAndStatus(Integer term, Integer status,
			Integer type) {
		return financingRepository.getFinancingByTermAndStatus(term,status,type);
	}
}
